
package com.oahcfly.chgame.core.ui;

import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.utils.ActorGestureListener;
import com.badlogic.gdx.scenes.scene2d.utils.Drawable;
import com.badlogic.gdx.utils.Align;
import com.oahcfly.chgame.core.freefont.FreeFont;
import com.oahcfly.chgame.core.freefont.FreeLabel;
import com.oahcfly.chgame.core.mvc.CHActor;

/**
 * 
 * 
 * 可以滚动的文本
 * 
 * @author haocao
 *
 */
public class CHScrollLabel extends CHActor {

	private FreeLabel freeLabel;
	// 滚动偏移量，左下角为(0,0)
	private float scrollY;

	private CHScrollLabelStyle scrollLabelStyle;

	public CHScrollLabel (CHScrollLabelStyle style, Color textColor, String text) {
		this(style, FreeFont.DEFAULT_FONT_NAME, textColor, text);
	}

	public CHScrollLabel (Color textColor, String text) {
		this(new CHScrollLabelStyle(), FreeFont.DEFAULT_FONT_NAME, textColor, text);
	}

	public CHScrollLabel (CHScrollLabelStyle style, String fontName, Color textColor, String text) {
		this.scrollLabelStyle = style;
		freeLabel = FreeFont.getLabel(textColor, text);
		freeLabel.setWrap(true);
		freeLabel.setAlignment(Align.topLeft);
		setText(text);
		setTextColor(textColor);

		addListener(new ActorGestureListener() {

			@Override
			public void pan (InputEvent event, float x, float y, float deltaX, float deltaY) {
				scrollY(deltaY);
			}

			@Override
			public void touchUp (InputEvent event, float x, float y, int pointer, int button) {
			}

		});
	}

	public CHScrollLabelStyle getScrollLabelStyle () {
		return scrollLabelStyle;
	}

	public void setScrollLabelStyle (CHScrollLabelStyle scrollLabelStyle) {
		this.scrollLabelStyle = scrollLabelStyle;
	}

	@Override
	public void drawAfterBg (Batch batch) {

		//背景
		if (scrollLabelStyle != null && scrollLabelStyle.background != null) {
			scrollLabelStyle.background.draw(batch, getX(), getY(), getWidth(), getHeight());
		}

		// 设置裁剪区域
		if (getClipRectangleArr().size == 0) {
			addClipRectangle(new Rectangle(0, 0, getWidth(), getHeight()));
		}

		freeLabel.setPosition(getX(), getY() + scrollY);
		freeLabel.setWidth(getWidth() - (scrollLabelStyle != null ? scrollLabelStyle.scrollBarWidth : 0));
		freeLabel.setHeight(getHeight());
		freeLabel.draw(batch, 1);

		Color color = getColor();
		batch.setColor(color.r, color.g, color.b, color.a * 0.8f);

		if (scrollLabelStyle != null && scrollLabelStyle.vScroll != null) {
			scrollLabelStyle.vScroll.draw(batch, getRight() - scrollLabelStyle.scrollBarWidth, getY(),
				scrollLabelStyle.scrollBarWidth, getHeight());
		}

		if (scrollLabelStyle != null && scrollLabelStyle.vScrollKnob != null) {
			// w=10,h=30,yRange=[gety~gettop-30]
			float scrollH = getTop() - scrollLabelStyle.scrollBarHeight - getY();
			float percent = scrollY / (freeLabel.getTextHeight() - getHeight());
			scrollLabelStyle.vScrollKnob.draw(batch, getRight() - scrollLabelStyle.scrollBarWidth, getY() + scrollH * (1 - percent),
				scrollLabelStyle.scrollBarWidth, scrollLabelStyle.scrollBarHeight);
		}
	}

	public void setText (String text) {
		freeLabel.setText(text);
	}

	public void setTextColor (Color textColor) {
		freeLabel.setColor(textColor);
	}

	public void scrollY (float value) {
		float temp = freeLabel.getTextHeight() - getHeight();
		if (temp <= 0) {
			return;
		}

		scrollY += value;

		// 边界检测
		if (scrollY > temp) {
			scrollY = temp;
		}
		if (scrollY < 0) {
			scrollY = 0;
		}
	}

	public FreeLabel getLabel () {
		return freeLabel;
	}

	/**
	 * 
	 * 
	 * 样式：背景，
	 * @author haocao
	 *
	 */
	static public class CHScrollLabelStyle {
		/** Optional. */
		public Drawable background;
		/** Optional. */
		public Drawable vScroll, vScrollKnob;

		public float scrollBarWidth = 10, scrollBarHeight = 30;

		public CHScrollLabelStyle () {
		}

		public CHScrollLabelStyle (Drawable background, Drawable vScroll, Drawable vScrollKnob) {
			this.background = background;
			this.vScroll = vScroll;
			this.vScrollKnob = vScrollKnob;
		}

		public CHScrollLabelStyle (CHScrollLabelStyle style) {
			this.background = style.background;
			this.vScroll = style.vScroll;
			this.vScrollKnob = style.vScrollKnob;
		}
	}
}
